<?php
namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use App\Models\UserModel;

class UserController extends Controller{

    public function __construct()
    {
        $this->middleware('user_login');
    }

    /**
     * 修改个人信息
     */
    public function edit_user(Request $request){

        $user = auth('user_api') -> user();

        $user->nickName =$request->input('nickName');
        $user->head_img =$request->input('head_img');
        $user->gender =$request->input('gender');
        $user->phone =$request->input('phone');
        $user->name =$request->input('name');
  /*      $user->id_card =$request->input('id_card');
        $user->deposit_bank =$request->input('deposit_bank');
        $user->bank_card =$request->input('bank_card');
        $user->email =$request->input('email');
        $user->birthday =$request->input('birthday');*/

        $res = $user->save();
        if(empty($res)){
            return returnMsg(400,'保存失败');
        }

        return returnMsg(0,'保存成功');
    }

    public function get_user(UserModel $userModel){

        //$user = auth('user_api') -> user();
        $data = $userModel
            ->select([
                'nickName',
                'head_img',
                'gender',
                'phone',
                'name',
                'id_card',
                'deposit_bank',
                'bank_card',
                'email',
                'birthday',
                'id_front',
                'id_back',
            ])
            ->where('id',auth('user_api') -> user()->id)
            ->first();

        $data->old_head_img = $data->getOriginal('head_img');
        $data->old_id_front = $data->getOriginal('id_front');
        $data->old_id_back = $data->getOriginal('id_back');

        return returnMsg(0,'获取成功',$data);

    }

    /**
     * 修改个人信息
     */
    public function edit_user_attestation(Request $request){

        $user = auth('user_api') -> user();
        /*$user->nickName =$request->input('nickName');
        $user->head_img =$request->input('head_img');
        $user->gender =$request->input('gender');
        $user->phone =$request->input('phone');*/
        $user->name =$request->input('name');
        $user->id_card =$request->input('id_card');
        $user->deposit_bank =$request->input('deposit_bank');
        $user->bank_card =$request->input('bank_card');
        $user->email =$request->input('email');
        $user->birthday =$request->input('birthday');
        $user->id_front =$request->input('id_front');
        $user->id_back =$request->input('id_back');

        $res = $user->save();
        if(empty($res)){
            return returnMsg(400,'保存失败');
        }

        return returnMsg(0,'保存成功');
    }

    //退出登录
    public function out_login(){
        auth('api')->logout();
        return returnMsg(0,'退出成功');
    }

}